//
//  CopingViewController.m
//
//  Created by 周成龙
//

#import "CopingViewController.h"

@interface CopingModel()

@end

@implementation CopingModel {
    NSMutableSet *_friends;
}

- (id) initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName {
    if (self = [super init]) {
        _firstName = firstName;
        _lastName = lastName;
        _friends = [NSMutableSet new];
    }
    return self;
}
- (void)addFriend:(CopingModel *)model {
    [_friends addObject:model];
}
- (void)removeFriend:(CopingModel *)model {
    [_friends removeObject:model];
}

- (id)copyWithZone:(NSZone *)zone {
    CopingModel *model = [[[self class] allocWithZone:zone] initWithFirstName:_firstName lastName:_lastName];
    model->_friends = [_friends mutableCopy];
    return model;
}

- (id)deepCopy {
    CopingModel *model = [[[self class] alloc] initWithFirstName:_firstName lastName:_lastName];
    // copyItems: YES（深拷贝）向每一个元素发送copy消息，用拷贝好的元素创建新的set，并将其返回给调用者。
    model->_friends = [[NSMutableSet alloc] initWithSet:_friends copyItems:YES];
    return model;
}

@end




@interface CopingViewController ()
@end

@implementation CopingViewController
#pragma mark - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    CopingModel *model = [[CopingModel alloc] initWithFirstName:@"Bob" lastName:@"Smith"];
    
    CopingModel *f1 = [[CopingModel alloc] initWithFirstName:@"B1" lastName:@"Smith"];
    [model addFriend:f1];
    NSLog(@"%p", f1);
    
    CopingModel *f2 = [[CopingModel alloc] initWithFirstName:@"B2" lastName:@"Smith"];
    [model addFriend:f2];
    NSLog(@"%p", f2);
    
    [model removeFriend:f2];
    model = [f2 copy];
    [model addFriend:f1];
    NSLog(@"%p, %p", f1, model);
    
    /**
     复制对象时需要决定采用浅拷贝还是深拷贝，一般情况下应该尽量执行浅拷贝
     如果你所写的对象需要深拷贝，那么可考虑新增一个专门执行深拷贝的方法
     */
}

#pragma mark - Private

#pragma mark - Delegate

#pragma mark - Setter / Getter


@end
